import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import {ConfigEnv, loadEnv} from 'vite';
import {resolve} from 'path';
import {createHtmlPlugin} from 'vite-plugin-html';
import purgeIcons from 'vite-plugin-purge-icons';
import {createSvgIconsPlugin} from "vite-plugin-svg-icons";
import path from 'path';

function pathResolve(dir: string) {
    return resolve(process.cwd(), '.', dir);
}

//purgeIcons

// https://vitejs.dev/config/
export default defineConfig(({command,mode}: ConfigEnv) => {
    const isBuild = command === 'build';
    //环境变量
    const env = loadEnv(mode, process.cwd());
    return {
        base: './',
        server:{
            hmr: true
        },
        plugins: [
            vue(),
            vueJsx(),
            purgeIcons(),
            createHtmlPlugin({
                inject: {
                    data: {
                        ...env
                    }
                }
            }),
            createSvgIconsPlugin({
                iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
                svgoOptions: isBuild,
                // default
                symbolId: 'icon-[dir]-[name]',
            })
        ],
        resolve: {
            alias: [
                {
                    find: /\/@\//,
                    replacement: pathResolve('src') + '/',
                },
                // /#/xxxx => types/xxxx
                {
                    find: /\/#\//,
                    replacement: pathResolve('types') + '/',
                },
            ]
        },
        css: {
            // css预处理器
            preprocessorOptions: {
                less: {
                    modifyVars: {
                        hack: `true; @import (reference) "${resolve("src/styles/index.less")}";`,
                    },
                    javascriptEnabled: true,
                },
            },

        },
        build:{
            assetsDir: './',
            target: 'es2015',
            cssTarget: 'chrome80',
            outDir: 'dist',
            // minify: 'terser',
            /**
             * 当 minify=“minify:'terser'” 解开注释
             * Uncomment when minify="minify:'terser'"
             */
            // terserOptions: {
            //   compress: {
            //     keep_infinity: true,
            //     drop_console: VITE_DROP_CONSOLE,
            //   },
            // },
            // Turning off brotliSize display can slightly reduce packaging time
            brotliSize: false,
            chunkSizeWarningLimit: 2000,
        },
        optimizeDeps: {
            // @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
            include: [
                '@vue/runtime-core',
                '@vue/shared',
                '@iconify/iconify',
                'ant-design-vue/es/locale/zh_CN',
                'ant-design-vue/es/locale/en_US',
            ],
        },

    }
})
